﻿using STMS.WSTMSystem.FModels;
using STMS.WSTMSystem.UControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem
{
    public partial class FrmMain : UCForm
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        int selFunCode = 1;//1-仓库管理  2-产品管理  3-仓库温控管理
        LoginData loginData = null;
        System.Timers.Timer timer = null;
        private void FrmMain_Load(object sender, EventArgs e)
        {
              if(this.Tag!=null)
            {
                loginData = this.Tag as LoginData;
                InitStatusInfo();
                InitUpBtns();
               ShowStoreManage();
                btnClosePage.Visible = true;
                btnMax.Text = "2";
            }
        }

        private void InitUpBtns()
        {
            upBtnStoreList.Tag = "Store.FrmStoreList";
            upBtnStoreRegionList.Tag = "Store.FrmStoreRegionList";
            upBtnProductList.Tag = "Product.FrmProductList";
            upBtnProductInStore.Tag = "Product.FrmProductInStore";
            upBtnStoreTemperList.Tag = "StoreTemper.FrmStoreTemperatureList";
        }

        private void ShowCloseBtn()
        {
            if (panelPage.Controls.Count>0)
                btnClosePage.Visible = true;
            else
                btnClosePage.Visible = false;
        }

        /// <summary>
        /// 打开仓库管理页面
        /// </summary>
        private void ShowStoreManage()
        {
            Store.FrmStoreList fStoreList = new Store.FrmStoreList();
            SetFunBtnsBackColor(1);
            panelPage.AddPanelPage(fStoreList);
            btnClosePage.Visible = true;
        }

        /// <summary>
        /// 初始化登录信息
        /// </summary>
        private void InitStatusInfo()
        {
            lblLoginUser.Text = loginData.UserName;
            //初始化计时器
            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.AutoReset = true;
            timer.Elapsed += Timer_Elapsed;
            timer.Start();
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            lblLoginTime.Invoke(new Action(() =>
            {
                lblLoginTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }));
        }

        /// <summary>
        /// 仓库管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStoreManage_Click(object sender, EventArgs e)
        {
            ShowStoreManage();
        }


        /// <summary>
        /// 产品管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProductManage_Click(object sender, EventArgs e)
        {
            SetFunBtnsBackColor(2);
            Product.FrmProductList fProductList = new Product.FrmProductList();
            panelPage.AddPanelPage(fProductList);
            btnClosePage.Visible = true;
        }

        /// <summary>
        /// 仓库温控管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStoreTemperManage_Click(object sender, EventArgs e)
        {
            SetFunBtnsBackColor(3);
            StoreTemper.FrmStoreTemperatureList fStoreTemperList = new StoreTemper.FrmStoreTemperatureList();
            panelPage.AddPanelPage(fStoreTemperList);
            btnClosePage.Visible = true;
        }

        /// <summary>
        /// 设置三个功能按钮的颜色切换
        /// </summary>
        private void SetFunBtnsBackColor(int code)
        {
            selFunCode = code;
            
            switch (selFunCode)
            {
                case 1:
                    SetBtnsColors(btnStoreManage, upBtnStoreList);
                    break;
                case 2:
                    SetBtnsColors(btnProductManage, upBtnProductList);
                    break;
                case 3:
                    SetBtnsColors(btnStoreTemperManage, upBtnStoreTemperList);
                    break;
            }
        }

        private void SetBtnsColors(UMenuButton btn,UPageButton btnPage)
        {
            Color unSelColor = Color.FromArgb(45, 50, 116);
            Color SelColor = Color.FromArgb(88, 116, 216);
            Color topSelColor = Color.FromArgb(45, 50, 116);
            Color topUnSelColor = Color.White;
            UMenuButton[] menuBtns = { btnStoreManage, btnProductManage, btnStoreTemperManage };
            UPageButton[] pageBtns = { upBtnStoreList, upBtnStoreRegionList, upBtnProductList, upBtnProductInStore, upBtnStoreTemperList };
            btn.SetBtnColor(SelColor);
            foreach (UMenuButton btnMenu in menuBtns)
            {
                if (btnMenu.Name == btn.Name)
                    btnMenu.SetBtnColor(SelColor);
                else
                    btnMenu.SetBtnColor(unSelColor);
            }
            foreach (UPageButton btnpage in pageBtns)
            {
                if (btnpage.Name == btnPage.Name)
                    btnpage.SetBtnColor(topSelColor);
                else
                    btnpage.SetBtnColor(topUnSelColor);
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MsgBoxHelper.MsgBoxConfirm("退出系统", "你确定要退出系统吗？") == DialogResult.Yes)
            {
                if(timer!=null)
                       timer.Stop();
                Application.ExitThread();
            }
            else
                e.Cancel = true;
        }

        /// <summary>
        /// 关闭页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClosePage_Click(object sender, EventArgs e)
        {
            foreach(Control c in panelPage.Controls)
            {
                if(c is Form)
                {
                    Form cf = c as Form;
                    if (cf.Visible)
                    {
                        panelPage.Controls.Remove(cf);
                        cf.Close();
                        break;
                    }
                }
            }
            if(panelPage.Controls.Count >0)
            {
                Form f = panelPage.Controls[0] as Form;
                f.Show();
                FormUtility.SetTopBtnFontColor(panelTopList, f);
            }
            else
            {
                FormUtility.SetAllTopBtnUnSelect(panelTopList);
            }
            ShowCloseBtn();
        }



        /// <summary>
        /// 关闭主窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.FrmMain_FormClosing(this, new FormClosingEventArgs(CloseReason.FormOwnerClosing, true));
        }
        private Point mousePoint = new Point();
        bool isMoving = false;
        private void panelTop_MouseDown(object sender, MouseEventArgs e)
        {
            mousePoint = e.Location;
            isMoving = true;
        }

        private void panelTop_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && isMoving)
            {
                Point posNew = new Point(e.Location.X - mousePoint.X, e.Location.Y - mousePoint.Y);
                this.Location += new Size(posNew);
            }
        }

        private void btnMin_Click(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Minimized)
                this.WindowState = FormWindowState.Minimized;
        }

        private void btnMax_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
                btnMax.Text = "1";
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
                btnMax.Text = "2";
            }
        }

        private void panelTop_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
                btnMax.Text = "1";
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
                btnMax.Text = "2";
            }
        }

        private void panelPage_SizeChanged(object sender, EventArgs e)
        {
                Size size = panelPage.Size;
            foreach (Control c in panelPage.Controls)
            {
                if (c is Form)
                {
                    Form f = c as Form;
                    f.WindowState = FormWindowState.Normal;
                    f.SuspendLayout();
                    f.Size = size;
                    f.ResumeLayout(true);
                    f.WindowState = FormWindowState.Maximized;
                   
                }

            }
        }

        /// <summary>
        /// 分页按钮响应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void upBtn_Click(object sender, EventArgs e)
        {
            UPageButton btn = sender as UPageButton;
            Form f = null;
            if (btn.Tag!=null)
            {
                string frmName = btn.Tag.ToString();
               f= FormUtility.GetOpenForm(frmName.Split('.')[1]);
               if(f==null)
                {
                    f = (Form)Activator.CreateInstance(Type.GetType(this.GetType().Assembly.GetName().Name + "." + frmName));
                }
            }
            panelPage.AddPanelPage(f);
         
            ShowCloseBtn();
        }
    }
}
